<?php

class Academico_ChangepassController extends Zend_Controller_Action {

    private $userInfo;

    //private $_debugLogger;

    public function init() {
        
    }

    /*
     * 
     */

    public function preDispatch() {
        // Autenticacion del usuario
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_helper->layout->disableLayout();
            $this->_forward("index", "index", "");
        } else {
            $identity = $auth->getIdentity();
            $this->userInfo['username'] = $identity['username'];
            if ($identity['tipo_usuario'] == VirtualLabs_Admin_Usuario::ROL_ADMIN) {
                $this->_helper->layout->disableLayout();
                $this->_redirect('/');
            }
        }
        //fin de la autenicacion del usuario
    }

    /*
     * 
     */

    public function indexAction() {
        $this->view->username = $this->userInfo['username'];
    }

    public function changepasswordAction() {
        $_logger = Zend_Registry::get(VirtualLabs_Common::DEBUG_LOG);
        $this->_helper->viewRenderer->setNoRender(true);
        $this->_helper->layout->disableLayout();
        $username = $this->userInfo['username'];
        $old_pass = $this->getRequest()->getParam("old_pass");
        $new_pass = $this->getRequest()->getParam("new_pass");
        $old_pass_md5 = VirtualLabs_Admin_Usuario::getPassword($old_pass);
        try {
            $usuarioDao = new VirtualLabs_Academico_Usuario();
            $usuario = $usuarioDao->getUsuario($username);

            if ($usuario['passw'] != $old_pass_md5) {
                throw new Exception("El password anterior es incorrecto");
            } else {
                $new_pass_md5 = VirtualLabs_Admin_Usuario::getPassword($new_pass);
                $usuarioDao->changePassw($username, $new_pass_md5);
            }
            echo Zend_Json_Encoder::encode(array('response' => 'success'));
        } catch (Exception $exc) {
            echo Zend_Json_Encoder::encode(array('response' => 'error', 'message' => $exc->getMessage()));
        }
    }

}

